<?php 
date_default_timezone_set('Asia/Shanghai'); 
//作者：QQ805500091 大淘客微信小程序定制：http://c.b1yt.com/h.k7bzat?cv=sjj7ZBFlbBt&sm=2596eb  或联系QQ
class Taokouling
{
    protected $putongpid;
    protected $queqiaopid;
    protected $appkey;
    protected $secretKey;
    protected $html_dom;
    public function __construct($appkey, $secretKey, $putongpid, $queqiaopid)
    {
        $this->appkey = $appkey;
        $this->secretKey = $secretKey;
        $this->putongpid = $putongpid;
        $this->queqiaopid = $queqiaopid;
    }
    function getHtml($html, $requestUrl, $param = array())
    {        
         require_once 'simple_html_dom.php';
         if (strstr($requestUrl, "r=p/d")) {
           $this ->html_dom  = str_get_html($html);
            return $this->doDetail($html);
        }
        return $html ;
    }
    function doDetail($html)
    {
        $html = $this->setTKL($html);
        return $html;
    }
    function setTKL($shtml)
    {
        function kouling($url, $tupian, $neirong, $appkey, $secretKey)
{
 
    $c = new TopClient ();
    $c->appkey =$appkey;
    $c->secretKey = $secretKey;

    $req = new WirelessShareTpwdCreateRequest ();
    $tpwd_param = new IsvTpwdInfo ();
    $tpwd_param->ext = "{\"xx\":\"xx\"}";
    if (!empty ($tupian)) {
        $tpwd_param->logo = $tupian;
    } else {
        $tpwd_param->logo = "http://f.hiphotos.baidu.com/zhidao/pic/item/b8014a90f603738d4c5c2242b51bb051f819ecb8.jpg";
    }
    if (!empty ($neirong)) {
        $tpwd_param->text = $neirong;
    } else {
        $tpwd_param->text = "粉丝福利购，立即领券~";
    }
    $tpwd_param->url = $url;
    $tpwd_param->user_id = "24234234234";
    $req->setTpwdParam(json_encode($tpwd_param));
    $resp = $c->execute($req);
    $koulong = $resp->model;
    return $koulong [0];
}

function html($html, $lianjie, $tupian, $neirong, $appkey, $secretKey)
{

    $kouling = kouling($lianjie["link"], $lianjie["img"], $lianjie["title"], $appkey, $secretKey);
    $html = str_ireplace('<div class="recommend-wrapper">', '<div class="recommend-wrapper">
<p class="am-text-warning am-text-default" style="padding: 5px 0;">【淘口令购买】长按框内 &gt; 全选 &gt; 复制</p>
<div class="detail-command-box am-panel" style="padding: 2px;">
<div class="am-panel-bd" style="border: 1px dashed #ff6a6a;padding: 8px;">
<span class="kl-tkl">长按复制框内整段文字，打开「手机淘宝」即可「领取优惠券」并购买' . $kouling . '</span></div>
<p class="am-text-warning" style="padding: 5px 0;">温馨提示：手机无【手机淘宝】者，可选择领券购买方式哦~</p>
</div> ', $html);
    return $html;
}

        $lianjie = $this->getLianJie($shtml);
        $lianjie = str_ireplace($this->putongpid, $this->queqiaopid, $lianjie);
        if (!empty($lianjie) || strpos($lianjie, 'uland.taobao.com') !== false) {
            include "topsdk/TopSdk.php";
            $tupian = "";
            $neirong = "";
            $appkey = $this->appkey;
            $secretKey = $this->secretKey;
            $ret = html($shtml, $lianjie, $tupian, $neirong, $appkey, $secretKey);
        } else {
            $ret = $shtml;
        }
        return $ret;
    }
    function getLianJie($html){

        try {
            $a = $this->html_dom->find('.img .ui-link', 0);
            $link = $a->href;
            $eTitle = $this->html_dom->find('.title-wrapper', 0);
            $eImage = $eTitle->prev_sibling();
            $src = 'http://f.hiphotos.baidu.com/zhidao/pic/item/b8014a90f603738d4c5c2242b51bb051f819ecb8.jpg';
            $title = trim($eTitle->plaintext);
        } catch (Exception $e) {
            $title = '';
            $src = '';
            $link = '';
        }
        return   array("title" =>$title , "img" => $src, "link" => $link );
}
}